package com.briup.jz.service.impl;

import com.briup.jz.bean.Article;
import com.briup.jz.bean.ArticleExample;
import com.briup.jz.bean.ArticleExample.Criteria;
import com.briup.jz.bean.extend.ArticleExtend;
import com.briup.jz.dao.ArticleMapper;
import com.briup.jz.dao.extend.ArticleExtendMapper;
import com.briup.jz.service.IArticleService;
import com.briup.jz.utils.CustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements IArticleService {

  @Autowired private ArticleMapper articleMapper;
  @Autowired private ArticleExtendMapper articleExtendMapper;

  @Override
  public List<Article> query(String title, String status, Long articleId) {
    // 模板查询， 你喜欢什么样的男孩子  男的 170 黑一点
    ArticleExample example = new ArticleExample();
    Criteria criteria = example.createCriteria();
    // 多条件符合查询0000
    if (title != null) {
      criteria.andTitleLike("%" + title + "%");
    }
    if (status != null) {
      criteria.andStatusEqualTo(status);
    }
    if (articleId != null) {
      criteria.andCategoryIdEqualTo(articleId);
    }
    // 返回查询结果
    return articleMapper.selectByExample(example);
  }

  @Override
  public List<ArticleExtend> queryCascade(String title, String status, Long categoryId) {
    return articleExtendMapper.select(title, status, categoryId);
  }

  @Override
  public ArticleExtend read(Long id) {
    // 1. 修改阅读次数

    // 2. 将要阅读的文字查询出来并且返回
    return articleExtendMapper.selectById(id);
  }

  @Override
  public void saveOrUpdate(Article article) {
    if (article.getId() == null) {
      // 初始化
      article.setPublishTime(new Date().getTime());
      article.setReadTimes(0l);
      article.setThumpUp(0l);
      article.setStatus("未审核");
      articleMapper.insert(article);
    } else {
      articleMapper.updateByPrimaryKey(article);
    }
  }

  @Override
  public void save(Article article) {
    // TODO Auto-generated method stub

  }

  @Override
  public List<Article> findAll() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Article findById(long id) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public List<Article> query(String name) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public void deleteById(long id) throws CustomerException {
    // TODO Auto-generated method stub

  }
}
